added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / VBExtractAudioFile / ReadMe.txt
blob75f95faccf7f4403c52dffc8bc9900bca5fb7b02
1 =============================================================================
2               Windows APPLICATION: VBExtractAudioFile Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
6 Summary:
8 The sample demonstrates how to extract and convert audio file formats, which 
9 include wav, mp3 and mp4 files.
11 The sample is used to extract music file formats. We usually play music with 
12 Windows Media Player or other music-playing software. If we find our favorite 
13 music clip, we can use the function of this sample to extract it and convert 
14 it into another file format. All technology mentioned above is based on 
15 Expression Encoder SDK 4.0. When you install Expression Encoder 4.0, you can 
16 use Visual Studio 2010 to add a reference to it. In this way, you don't have 
17 to install the SDK installation kits individually.
19 The sample uses Expression Encoder SDK 4 to output *.mp4 or *.wma file. The 
20 .mp3 audio format is currently not supported as output format in Expression 
21 Encoder 4.
24 /////////////////////////////////////////////////////////////////////////////
25 Prerequisite:
27 1. Please install a free version, called "Microsoft Expression Encoder 4" at 
28 the link as follows:
29 http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=75402be0-c603-4998-a79c-becdd197aa79
31 The free version is a feature-filled VC-1 encoding application that supports 
32 the following: 
34     - High performance multi-core encoding 
35     - Crop/scale/de-interlace operation 
36     - Multi-clip editing 
37     - A/B compare 
38     - Live encoding 
39     - Up to 10 minutes of screen capture 
40     - Smart encoding 
41     - Silverlight templates 
42     - Multi-channel audio import and export 
43     - Rich metadata support 
44     - Presets and custom plug-ins as well as 
45     - Full access to the .NET SDK for all above features 
46     - VC-1 Smooth Streaming (new for v4) 
48 The Professional version of Microsoft Expression Encoder 4 adds:
49     - H.264 encoding (both MP4 and Smooth Streaming) 
50     - Live Smooth Streaming encoding 
51     - DRM 
52     - Native support of MP4/H.264, TS, M2TS, AVCHD, MPEG-2, ISM, ISMV, AAC 
53       and AC-3 files
54     - Unlimited screen capture durations 
56 So if you like these features provided by this software application, please 
57 purchase the professional version to support more advanced features.
60 /////////////////////////////////////////////////////////////////////////////
61 Demo:
63 Step1. Install Microsoft Expression Encoder 4. 
65 Step2. Build and run the sample project in Visual Studio 2010. 
67 Step3. Prepare a .wma or .mp4 or .mp3 audio file.  Click the button which is 
68        written as "Choose Audio File..." and select the audio file. 
70 Step4. Click the play button and drag the block on the progress bar to 
71        seek the position that you want to start extracting.  Then, you click 
72        the "Set Start Point" button.
73        Do the same operation to set the end of extract file.  It's important 
74        that you don't set the extract end point before the start point.
76 Step5. Select the audio output file format between WMA and MP4. Set the output 
77        directory.
79 Step6. Click the "Extract" button to extract the audio file.  If it succeeds, 
80        you will be informed the output file path.  You can play the output 
81        audio file to check if the audio clip is correct. 
84 /////////////////////////////////////////////////////////////////////////////
85 Implementation:
87 1. The sample uses the Windows Media Player control to play the source audio 
88    file.  The following MSDN article introduces how to embed the Windows 
89    Media Player ActiveX control in a Windows Form.
90    http://msdn.microsoft.com/en-us/library/dd562851.aspx
91    
92    We play a specified audio file in the Windows Media Player control by 
93    setting its URL property and calling AxWindowsMediaPlayer.Ctlcontrols.play().
95     Me.player.URL = openAudioFileDialog.FileName
96     Me.player.Ctlcontrols.play()
98    When the form is being closed, we call the AxWindowsMediaPlayer.close() 
99    method to release Windows Media Player resources.
101     Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
102         ' Releases Windows Media Player resources.
103         Me.player.close()
104     End Sub
106    In order to get the current playing position in the media item, and save 
107    the start and end points, we use the 
108    AxWindowsMediaPlayer.Ctlcontrols.currentPosition property. It contains the 
109    current position in the media item in seconds from the beginning:
111     If btnSetBeginEndPoints.Tag.Equals("SetStartPoint") Then
112      ' Save the startpoint.
113      ' player.Ctlcontrols.currentPosition contains the current 
114      ' position in the media item in seconds from the beginning.
115      Me.tbStartpoint.Text = (player.Ctlcontrols.currentPosition * 1000).ToString("0")
116      Me.tbEndpoint.Text = ""
118      Me.btnSetBeginEndPoints.Text = "Set End Point"
119      Me.btnSetBeginEndPoints.Tag = "SetEndPoint"
120     ElseIf btnSetBeginEndPoints.Tag.Equals("SetEndPoint") Then
121      ' Check if the startpoint is in front of the endpoint.
122        Dim endPoint As Integer = CInt(Fix(player.Ctlcontrols.currentPosition * 1000))
123        If endPoint <= Integer.Parse(Me.tbStartpoint.Text) Then
124          MessageBox.Show("Audio endpoint is overlapped. Please reset the endpoint.")
125          Else
126           ' Save the endpoint.
127           Me.tbEndpoint.Text = endPoint.ToString()
129           Me.btnSetBeginEndPoints.Text = "Set Start Point"
130           Me.btnSetBeginEndPoints.Tag = "SetStartPoint"
131        End If
132    End If
133       
134 2. The sample uses following helper function to cut the audio file from 
135    startpoint to endpoint, and output the clip as the selected audio format.  
137     Public Shared Function ExtractAudio(ByVal sourceAudioFile As String, ByVal outputDirectory As String, ByVal outputType As OutputAudioType, ByVal startpoint As Double, ByVal endpoint As Double) As String
138         Using [job] As New Job()
139             Dim src As New MediaItem(sourceAudioFile)
140             Select Case outputType
141                 Case OutputAudioType.MP4
142                     src.OutputFormat = New MP4OutputFormat()
143                     src.OutputFormat.AudioProfile = New AacAudioProfile()
144                     src.OutputFormat.AudioProfile.Codec = AudioCodec.AAC
145                     src.OutputFormat.AudioProfile.BitsPerSample = 24
146                 Case OutputAudioType.WMA
147                     src.OutputFormat = New WindowsMediaOutputFormat()
148                     src.OutputFormat.AudioProfile = New WmaAudioProfile()
149                     src.OutputFormat.AudioProfile.Bitrate = New VariableConstrainedBitrate(128, 192)
150                     src.OutputFormat.AudioProfile.Codec = AudioCodec.WmaProfessional
151                     src.OutputFormat.AudioProfile.BitsPerSample = 24
152             End Select
154             Dim spanStart As TimeSpan = TimeSpan.FromMilliseconds(startpoint)
155             src.Sources(0).Clips(0).StartTime = spanStart
156             Dim spanEnd As TimeSpan = TimeSpan.FromMilliseconds(endpoint)
157             src.Sources(0).Clips(0).EndTime = spanEnd
159             [job].MediaItems.Add(src)
160             [job].OutputDirectory = outputDirectory
161             [job].Encode()
163             Return [job].MediaItems(0).ActualOutputFileFullPath
164         End Using
165     End Function
166    End Class
168    btnExtract_Click in MainForm.cs calls the helper method to extract the audio file.
170    Dim outputType As OutputAudioType = CType(Me.cmbOutputAudioType.SelectedValue, OutputAudioType)
171    Dim outputFileName As String = ExpressionEncoderWrapper.ExtractAudio(sourceAudioFile, outputDirectory, outputType, Double.Parse(startpoint), Double.Parse(endpoint))
173            
175 //////////////////////////////////////////////////////////////////////////////////////
176 References:
178 Expression Encoder SDK Programming Reference 
179 http://msdn.microsoft.com/en-us/library/ff396833(v=Expression.40).aspx
181 Microsoft Expression Encoder 4 FAQ 
182 http://social.expression.microsoft.com/Forums/en/encoder/thread/3eabf903-b49f-4f92-b508-f28a795d6c90
185 //////////////////////////////////////////////////////////////////////////////////////